package com.gmail.blackdog1987.carcare.adapter;

import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TProduct;

public class ProductAdapter extends BaseAdapter {

	List<TProduct> datas;
	Context mContext;
	ImageLoader imageLoader;
	private static Map<String, Integer> type = new HashMap<String, Integer>();
	static{
		type.put("t_order_carchange", R.drawable.bg_main_change);
		type.put("t_order_carfix", R.drawable.bg_main_fix);
		type.put("t_order_carrent", R.drawable.bg_main_carrent);
		type.put("t_order_carsale", R.drawable.bg_main_carsale);
		type.put("t_order_clean", R.drawable.bg_main_clean);
		type.put("t_order_drivecompany", R.drawable.bg_main_company);
		type.put("t_order_driveschool", R.drawable.bg_main_jiazhao);
		type.put("t_order_maintain", R.drawable.bg_main_meirong);
		type.put("t_order_roadhelp", R.drawable.bg_main_roadhelp);
		type.put("t_order_rules_check", R.drawable.bg_main_breakrule);
	}

	public ProductAdapter(Context context) {
		this.mContext = context;
		imageLoader = ImageLoaderFactory.create(mContext);
	};
	
	public void addDatas(List<TProduct> datas){
		if(this.datas==null){
			this.datas = new ArrayList<TProduct>();
		}
		this.datas.addAll(datas);
	}
	
	public void setDatas(List<TProduct> datas){
		if(this.datas==null){
			this.datas = new ArrayList<TProduct>();
		}
		this.datas.clear();
		this.datas.addAll(datas);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {

		return datas==null?0:datas.size();
	}

	@Override
	public TProduct getItem(int position) {
		return datas==null?null:datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final TProduct m = getItem(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_product, null);
			holder = new ViewHolder();
			holder.mHead = (ImageView) convertView.findViewById(R.id.item_product_img);
			holder.mPrice = (TextView) convertView.findViewById(R.id.item_product_price);
			holder.mSubject = (TextView) convertView.findViewById(R.id.item_product_subject);
			holder.mTime = (TextView) convertView.findViewById(R.id.item_product_time);
			holder.mStatus = (TextView) convertView.findViewById(R.id.item_product_status);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Integer t = type.get(m.tableName);
		if(t==null||t==0){
			
		}else{
			holder.mHead.setImageResource(t);
		}
		holder.mPrice.setText(String.valueOf(m.getPrice()));
		holder.mSubject.setText(m.getSubject());
		holder.mTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(m.getCreatetime())));
		if(m.status==null||m.status==0){
			holder.mStatus.setTextColor(mContext.getResources().getColor(R.color.red));
			holder.mStatus.setText("未支付");
		}else{
			holder.mStatus.setTextColor(mContext.getResources().getColor(android.R.color.black));
			holder.mStatus.setText("已支付");
		}
		convertView.setTag(holder);
		return convertView;
	}

	public class ViewHolder {
		TextView mSubject;
		TextView mPrice;
		TextView mTime;
		TextView mStatus;
		ImageView mHead;
	}

}
